using System;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class RatePopup : MonoBehaviour
{
	public enum RatePopupStatus
	{
		None = 0,
		Showed = 1,
		Remind = 2,
		Ignore = 3
	}

	public static RatePopup instance;

	private string title = "Do you enjoy playing fisp.io?";

	private string message = "Your support with ratings and reviews can help us grow the game.";

	private string rate = "Rate Now";

	private string later = "Later";

	private string no = "No, thanks";

	public GameObject panel;

	public TextMeshProUGUI titleText;

	public TextMeshProUGUI messageText;

	public TextMeshProUGUI rateText;

	public TextMeshProUGUI laterText;

	public TextMeshProUGUI noText;

	public Button rateButton;

	public Button laterButton;

	public Button noButton;

	public Button rateButtonAtMenu;

	public int RateSessionCount
	{
		get
		{
			return PlayerPrefs.GetInt("RateSessionCount", 0);
		}
		set
		{
			PlayerPrefs.SetInt("RateSessionCount", value);
		}
	}

	public RatePopupStatus RateStatus
	{
		get
		{
			return (RatePopupStatus)Enum.Parse(typeof(RatePopupStatus), PlayerPrefs.GetString("RateStatus", "None"));
		}
		private set
		{
			PlayerPrefs.SetString("RateStatus", value.ToString());
		}
	}

	public string RemindDate
	{
		get
		{
			return PlayerPrefs.GetString("RemindDate", DateTime.Now.ToString("M/d/yyyy"));
		}
		set
		{
			PlayerPrefs.SetString("RemindDate", value);
		}
	}

	private void Awake()
	{
		if (instance == null)
		{
			instance = this;
		}
		else if (instance != this)
		{
			UnityEngine.Object.Destroy(base.gameObject);
		}
		titleText.text = title;
		messageText.text = message;
		rateText.text = rate;
		laterText.text = later;
		noText.text = no;
		rateButton.onClick.AddListener(delegate
		{
			RateStatus = RatePopupStatus.Showed;
			AnalyticManager.SendEvent("RateMyApp_PopupRate_Yes");
			Utils_OnSessionCountChanged();
			Application.OpenURL(Utils.StorePageURLRate);
			panel.SetActive(false);
		});
		laterButton.onClick.AddListener(delegate
		{
			RateStatus = RatePopupStatus.Remind;
			AnalyticManager.SendEvent("RateMyApp_PopupRate_Later");
			RateSessionCount = Utils.instance.SessionCount;
			RemindDate = DateTime.Now.ToString("M/d/yyyy");
			Utils_OnSessionCountChanged();
			panel.SetActive(false);
		});
		noButton.onClick.AddListener(delegate
		{
			RateStatus = RatePopupStatus.Ignore;
			RateSessionCount = Utils.instance.SessionCount;
			AnalyticManager.SendEvent("RateMyApp_PopupRate_No");
			Utils_OnSessionCountChanged();
			panel.SetActive(false);
		});
		if (rateButtonAtMenu != null)
		{
			rateButtonAtMenu.onClick.AddListener(delegate
			{
				RateStatus = RatePopupStatus.Showed;
				Application.OpenURL(Utils.StorePageURLRate);
				AnalyticManager.SendEvent("RateMyApp_FromMainMenu");
				rateButtonAtMenu.gameObject.SetActive(false);
			});
		}
	}

	private void OnEnable()
	{
		Utils.OnSessionCountChanged += Utils_OnSessionCountChanged;
	}

	private void OnDisable()
	{
		Utils.OnSessionCountChanged -= Utils_OnSessionCountChanged;
	}

	private void Utils_OnSessionCountChanged()
	{
		if (rateButtonAtMenu != null)
		{
			if (RateStatus == RatePopupStatus.Remind)
			{
				rateButtonAtMenu.gameObject.SetActive(true);
			}
			else if (RateStatus == RatePopupStatus.Ignore && Utils.instance.SessionCount - RateSessionCount >= 6)
			{
				rateButtonAtMenu.gameObject.SetActive(true);
			}
			else
			{
				rateButtonAtMenu.gameObject.SetActive(false);
			}
		}
	}

	public void ShowPopup()
	{
		panel.SetActive(true);
	}

	public bool CanShowRate(float gameLenght)
	{
		if (RateStatus == RatePopupStatus.None && gameLenght > (float)Values.Instance.rate.MinGameTime && Utils.instance.SessionCount - RateSessionCount >= Values.Instance.rate.AfterSession)
		{
			return true;
		}
		if (RateStatus == RatePopupStatus.Remind && gameLenght > (float)Values.Instance.laterRate.MinGameTime && Utils.instance.SessionCount - RateSessionCount >= Values.Instance.laterRate.AfterSession && !RemindDate.Equals(DateTime.Now.ToString("M/d/yyyy")))
		{
			return true;
		}
		return false;
	}
}
